package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.constant.MessageConstant;
import com.itcast.dao.OrdersettingDao;
import com.itcast.entity.Result;
import com.itcast.pojo.OrderSetting;
import com.itcast.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrdersettingService.class )
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    private OrdersettingDao ordersettingDao;
    @Override
    public void add(String dateString, String cell) throws ParseException {

//            Date date = new Date(dateString);
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
            /*String sdf = new SimpleDateFormat("yyyy-MM-dd").format(date);
            date=new SimpleDateFormat("yyyy-MM-dd").parse(sdf);*/
            int number = Integer.parseInt(cell);
            Integer count = ordersettingDao.findCount(date);
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setOrderDate(date);
            orderSetting.setNumber(number);
            if (count>0){

                ordersettingDao.updateByOrderDate(orderSetting);

            }else {
                ordersettingDao.add(orderSetting);
            }

    }


    /**
     * 所有套餐预约信息
     * @return
     */
    @Override
    public List<Map<String,Object>> findAll(String stringDate) {

 /*this.leftobj = [
                        { date: 1, number: 120, reservations: 1 },
                        { date: 3, number: 120, reservations: 1 },
                        { date: 4, number: 120, reservations: 120 },
                        { date: 6, number: 120, reservations: 1 },
                        { date: 8, number: 120, reservations: 1 }
                    ];*/
        if (stringDate!=null &&  stringDate.length()>0){
            stringDate = stringDate + "%";
        }else {
            stringDate = "2020-01%";
        }
        List<OrderSetting> all = ordersettingDao.findAll(stringDate);
        List<Map<String,Object>> list = new ArrayList<>();
        for (OrderSetting orderSetting : all) {
            Date orderDate = orderSetting.getOrderDate();
            /*String date = getdate(orderDate);
            int da = Integer.parseInt(date);
            if (da<10){
               date = date.substring(1);
            }*/
            /**
             * Calendar 获取日期需要先获取Calendar对象
             */
            Calendar instance = Calendar.getInstance();
            instance.setTime(orderDate);
            int date = instance.get(Calendar.DAY_OF_MONTH);
            int ordernumber = orderSetting.getNumber();
            int orderreservations = orderSetting.getReservations();
            Map<String,Object> map = new HashMap<>();
            map.put("date",date);
            map.put("number",ordernumber);
            map.put("reservations",orderreservations);
            list.add(map);

        }

        return list;
    }

    @Override
    public Result editNumber(OrderSetting orderSetting) throws ParseException {
        if (orderSetting!=null ){
            Date orderDate = orderSetting.getOrderDate();
            Integer count = ordersettingDao.findCount(orderDate);

            if (count>0){
                ordersettingDao.updateByOrderDate(orderSetting);
                return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
            }else {
                ordersettingDao.add(orderSetting);
                return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
            }
        }else {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    private String getdate (Date orderDate){
        String format = new SimpleDateFormat("yyyy-MM-dd").format(orderDate);
        int index = format.lastIndexOf("-");
        index=index+1;
        String date = format.substring(index);
        System.out.println(date);
        return date;
    }
}
